<template>
  <div class="article-page">
    <!-- 上边导航 -->
    <nav class="my-nav van-hairline--bottom">
      <a href="javascript:;" :class="{active: sorter === 'weight_desc'}" @click="changeSorter('weight_desc')">推荐</a>
      <!-- axios发请求, 如果属性的值为null或者undefined, 那么axios会排除掉该属性 -->
      <a href="javascript:;"  :class="{active: sorter === null}"  @click="changeSorter(null)">最新</a>
      <div class="logo"><img src="@/assets/logo.png" alt="" /></div>
    </nav>

    <van-list
      v-model="loading"
      :finished="finished"
      finished-text="没有更多了"
      @load="onLoad"
    >
      <ArticleItem
        v-for="item in list"
        :item="item"
        :key="item.id"
      ></ArticleItem>
    </van-list>
  </div>
</template>

<script>
import { getArticles } from '@/api/article'
export default {
  name: 'article-page',
  data () {
    return {
      list: [], // 数据数组
      current: 1,
      sorter: 'weight_desc',
      loading: false, // 控制加载状态
      finished: false // 控制是否全部加载完成
    }
  },
  methods: {
    async onLoad () {
      // 1. 发请求
      const res = await getArticles({
        current: this.current,
        sorter: this.sorter
      })
      // console.log(res)
      // this.list = res.data.rows
      this.list.push(...res.data.rows)
      // [1, 2, 3].push(...[4, 5, 6])

      // 2. 修改加载状态
      this.loading = false

      // 3. 让当前页码++
      this.current++

      // 4. 判断是否全部加载完毕了
      if (this.current > res.data.pageTotal) {
        this.finished = true
      }
    },
    changeSorter (sorter) {
      // console.log(sorter)
      // 1.1 修改频道的值
      this.sorter = sorter

      // 1.2 重置所有的数据
      this.current = 1
      this.list = []
      this.finished = false
      // this.loading = false

      // 1.3 手动触发请求
      // 手动调用了加载更多，也需要手动将loading改成true，表示正在加载中（避免重复触发）
      this.loading = true
      this.onLoad()
    }
  }
}
</script>

<style lang="less" scoped>
.article-page {
  margin-bottom: 50px;
  margin-top: 44px;
  .my-nav {
    height: 44px;
    position: fixed;
    left: 0;
    top: 0;
    width: 100%;
    z-index: 999;
    background: #fff;
    display: flex;
    align-items: center;
    > a {
      color: #999;
      font-size: 14px;
      line-height: 44px;
      margin-left: 20px;
      position: relative;
      transition: all 0.3s;
      &::after {
        content: "";
        position: absolute;
        left: 50%;
        transform: translateX(-50%);
        bottom: 0;
        width: 0;
        height: 2px;
        background: #222;
        transition: all 0.3s;
      }
      &.active {
        color: #222;
        &::after {
          width: 14px;
        }
      }
    }
    .logo {
      flex: 1;
      display: flex;
      justify-content: flex-end;
      > img {
        width: 64px;
        height: 28px;
        display: block;
        margin-right: 10px;
      }
    }
  }
}
</style>
